package no.hib.mod250.chat.client.view;

import no.hib.mod250.chat.client.controller.ChatController;
import no.hib.mod250.chat.client.i18.ChatConstants;
import no.hib.mod250.chat.client.model.Model;
import no.hib.mod250.chat.client.util.ObjectWithEvent;
import no.hib.mod250.chat.client.util.R;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ChatView extends View {

	// CLASSES

	public interface MessageClickObserver {

		public void onMessageClick(Model.Message message);

	}

	private class HeaderComposite extends Composite {

		private HorizontalPanel panel = new HorizontalPanel();
		private Image logo_hib = new Image(R.Image.logo_hib);
		private Image logo_gwt = new Image(R.Image.logo_gwt);
		private HTML header = new HTML();

		public HeaderComposite() {
			initWidget(panel);

			panel.setStyleName(R.Class.Header.header);
			logo_hib.setStyleName(R.Class.Header.logo_hib);
			logo_gwt.setStyleName(R.Class.Header.logo_gwt);

			header.setHTML("<h1>" + chatConstants.header() + "</h1>");

			panel.add(logo_hib);
			panel.add(logo_gwt);
			panel.add(header);
			panel.setCellVerticalAlignment(logo_hib,
					HasVerticalAlignment.ALIGN_MIDDLE);
			panel.setCellVerticalAlignment(logo_gwt,
					HasVerticalAlignment.ALIGN_MIDDLE);
			panel.setCellVerticalAlignment(header,
					HasVerticalAlignment.ALIGN_MIDDLE);
		}

	}

	// /CLASSES

	// VARIABLES

	// ... VIEW

	private Panel panel = new FlowPanel();
	private StatusView statusView;
	private LoginView loginView;
	private MessageWindowView messageWindowView;
	private UsersView usersView;
	private HeaderComposite headerComposite = new HeaderComposite();

	// ... /VIEW

	public static ChatConstants.Chat chatConstants = GWT
			.create(ChatConstants.Chat.class);

	// /VARIABLES

	// CONSTRUCTOR

	public ChatView(ChatController controller) {
		super(controller);

		statusView = new StatusView(controller);
		loginView = new LoginView(controller);
		messageWindowView = new MessageWindowView(controller);
		usersView = new UsersView(controller);

		initWidget(panel);

		doDraw();
		doEvent();
	}

	// /CONSTRUCTOR

	// FUNCTIONS

	// ... DO

	public void doDraw() {
		HorizontalPanel verticalPanel_2 = new HorizontalPanel();
		panel.add(verticalPanel_2);
		verticalPanel_2.setSize("100%", "");

		verticalPanel_2.add(headerComposite);
		verticalPanel_2.setCellHeight(headerComposite, "100%");

		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_2);
		horizontalPanel_2.setSize("100%", "");
		verticalPanel_2.setCellHeight(horizontalPanel_2, "100%");
		verticalPanel_2.setCellWidth(horizontalPanel_2, "25%");
		verticalPanel_2.setCellHorizontalAlignment(horizontalPanel_2,
				HasHorizontalAlignment.ALIGN_RIGHT);

		Widget loginPanel = loginView;
		horizontalPanel_2.add(loginPanel);
		loginView.setSize("100%", "");
		horizontalPanel_2.setCellHeight(loginPanel, "100%");
		horizontalPanel_2.setCellWidth(loginPanel, "100%");
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		panel.add(horizontalPanel);
		horizontalPanel.setSize("100%", "100%");

		Widget messageWindow = messageWindowView;
		horizontalPanel.add(messageWindow);
		horizontalPanel.setCellHeight(messageWindow, "100%");
		messageWindowView.setSize("100%", "");

		VerticalPanel verticalPanel_1 = new VerticalPanel();
		horizontalPanel.add(verticalPanel_1);
		horizontalPanel.setCellWidth(verticalPanel_1, "25%");
		verticalPanel_1.setSize("100%", "");

		verticalPanel_1.add(usersView);
		verticalPanel_1.setCellWidth(usersView, "100%");
		usersView.setSize("100%", "");

		// Add Status view
		panel.add(statusView);

		doTitleUpdate(0);
	}

	private void doEvent() {

		// Handle Unread Messages counter
		getController().getUnreadCounter().addListener(
				new ObjectWithEvent.Listener<Integer>() {

					@Override
					public void onChanged(Integer value) {
						doTitleUpdate(value);
					}

				});

	}

	private void doTitleUpdate(int unreadCounter) {
		if (unreadCounter == 0) {
			Window.setTitle("Chat Prototype");
		} else {
			Window.setTitle("[" + unreadCounter + "] Chat Prototype");
		}
	}

	// ... /DO

	// /FUNCTIONS

}
